

//
//  FeedBackTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/2.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "FeedBackTabVC.h"
#import "WriteRequirementTitleCell.h"
#import "TextViewCell.h"
#import "UITextView+Placeholder.h"
#import "ButtonCell.h"
#import "TextFieldCell.h"
@interface FeedBackTabVC ()<ButtonCellDelegate,TextViewCellDelegate,TextFieldCellDelegate>
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) NSString *content;
@property(nonatomic,strong) NSString *phoneS;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@end
//static NSString *identifierTitle = @"WriteRequirementTitleCell";
//static NSString *identifierNamed = @"TextViewCell";
//static NSString *identifierBtn = @"ButtonCell";
//static NSString *identifierTextField = @"TextFieldCell";
@implementation FeedBackTabVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户反馈";
    self.manager = [AFHTTPSessionManager manager];
//    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierTitle];
//    [self.tableView registerNib:[UINib nibWithNibName:@"TextViewCell" bundle:nil] forCellReuseIdentifier:identifierNamed];
//    [self.tableView registerNib:[UINib nibWithNibName:@"TextFieldCell" bundle:nil] forCellReuseIdentifier:identifierTextField];
//    [self.tableView registerNib:[UINib nibWithNibName:@"ButtonCell" bundle:nil] forCellReuseIdentifier:identifierBtn];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userfeedback:) name:@"userfeedback" object:nil];
    
}

-(void)userfeedback:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [WSProgressHUD dismiss];
    });
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)reloadInternetData
{
    XLog(@"self.dict:%@",self.dict);
    if (self.content.length == 0) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请填写您要反馈的信息"];
    }else if (self.phoneS.length == 0) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请填写您的联系方式"];
    }else {
        self.dict = @{
                      @"id":@"",
                      @"uid":@"1",
                      @"content":self.content,
                      @"phone":self.phoneS
                      };
        XLog(@"self.dict:%@",self.dict);
        [CustomeManager POSTWithURLIndex:32 parameters:self.dict success:^(NSDictionary *dict) {
            
        } failure:^(NSError *error) {
            
        }];
        
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0||indexPath.row == 2) {
            WriteRequirementTitleCell *cell = [WriteRequirementTitleCell cellNibWithTableView:tableView];
            if (indexPath.row == 0) {
                cell.titleL.text = @"感谢您的反馈：";
            }else {
                cell.titleL.text = @"请让我们回复您吧：";
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else if (indexPath.row == 1) {
            TextViewCell *cell = [TextViewCell cellNibWithTableView:tableView];
            cell.textView.placeholder = @"请在这里写下您的宝贵意见吧，它是我们进步的源泉哦～";
            cell.textView.layer.cornerRadius = 3;
            cell.textView.layer.borderColor  = kLineColor.CGColor;
            cell.textView.layer.borderWidth  = 1;
            cell.delegate = self;
            return cell;
        }else if (indexPath.row == 3) {
            TextFieldCell *cell = [TextFieldCell cellNibWithTableView:tableView];
            cell.tf.layer.cornerRadius = 3;
            cell.tf.layer.borderColor  = kLineColor.CGColor;
            cell.tf.layer.borderWidth  = 1;
            cell.tf.borderStyle = UITextBorderStyleNone;
            cell.lineL.hidden = YES;
            cell.delegate = self;
            cell.tf.placeholder = @"请留下您的邮箱或电话吧，以便我们回复您呐～";
            return cell;
        }else {
            ButtonCell *cell = [ButtonCell cellNibWithTableView:tableView];
            [cell.btn setTitle:@"发送" forState:UIControlStateNormal];
            cell.delegate = self;
            return cell;
        }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0||indexPath.row == 2) {
        return 60;
    }else if(indexPath.row == 1||indexPath.row == 4) {
        return 200;
    }else{
        return 40;
    }
    
}
#pragma mark-代理:反馈
- (void)TextViewCell:(TextViewCell *)TextViewCell withTextView:(nonnull UITextView *)textView withIndexPath:(nonnull NSIndexPath *)indexPath
{
    self.content = textView.text;
}
#pragma mark-代理:手机号
-(void)textFieldCell:(TextFieldCell *)textFieldCell withTextField:(UITextField *)tf withIndexPath:(NSIndexPath *)indexPath
{
    self.phoneS = tf.text;
}
#pragma mark-代理---下一步
- (void)buttonCell:(ButtonCell *)buttonCell withClickBtn:(UIButton *)clickBtn
{
    if ([[UserDefaultsHelper readAutoLogIn] isEqualToString:@"NO"]) {
        [LogOutAccount presentOutAccount];
        [UIAlertViewWarningAccount showWarningAccount];
    }else if ([UserDefaultsHelper readAutoLogIn].length == 0) {
        [LogOutAccount presentOutAccount];
    }else {
        self.dict = @{
                      @"userId":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken]
                      };
        self.urlString = [NSString stringWithFormat:@"%@/user/checkToken",kUrl];
        [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
            if (status == 200) {
                [self reloadInternetData];
            }else {
                [UIAlertViewWarningAccount showWarningAccount];
                [LogOutAccount presentOutAccount];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
    }
}
@end
